import logging
import os
from logging.handlers import RotatingFileHandler


def router_log(name):
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)  # 设置日志级别

    # 日志文件名
    base_dir = os.path.split(os.path.realpath(__file__ + "/../../"))[0]
    path = os.path.join(os.path.join(base_dir, 'logfile'), 'real_time.log')
    fh = RotatingFileHandler(path, maxBytes=1024 * 1024 * 100, backupCount=5, encoding='utf-8')
    fh.namer = lambda x: "backup." + x.split(".")[-1]

    fmt = logging.Formatter("%(asctime)s %(levelname)s %(funcName)s：%(lineno)d  %(pathname)s  %(message)s")  # 日志输出格式
    fh.setFormatter(fmt)
    logger.addHandler(fh)

    return logger


if __name__ == '__main__':
    base_dir = os.path.split(os.path.realpath(__file__ + "/../../"))[0]
    path = os.path.join(os.path.join(base_dir, 'logfile'), 'real_time.log')
    print(path)
